function sumMax1(arr, n) {
  let end = n
  let max = 0, sum = 0, point = 0
  for (let i = 0; i < n; i++) {
    max += arr[i]
  }
  for (let j = 1; j < arr.length; j++) {
    end++
    sum = 0
    for (let n = j; n < end; n++) {
      sum += arr[n]
    }
    if (sum > max) {
      max = sum
      point = end - n
    }
  }
  return {max1: max, point1: point}
}

function sumMax2(arr, n) {
  let max = 0, sum = 0, point = 0
  for (let i = 0; i < arr.length; i++) {
    if (i === 0) {
      for (let j = 0; j < n; j++) {
        sum += arr[j]
      }
    }else {
      sum = sum - arr[i - 1] + arr[i + n - 1]
    }
    if (sum > max) {
      max = sum
      point = i
    }
  }
  return {max2: max, point2: point}
}
let arr = []
for(let i = 0; i < 15; i++) {
  arr.push(Math.floor(Math.random() * 40))
}
const {max1, point1} = sumMax1(arr, 2)
const {max2, point2} = sumMax2(arr, 2)
console.log('方法一:', arr, arr.slice(point1, point1 + 2), max1)
console.log('方法二:', arr, arr.slice(point2, point2 + 2), max2)
